#!/bin/bash
# EKF2 Missing Data Troubleshooting Script
# This script helps diagnose and fix "preflight fail ekf2 missing data" error

echo "=== EKF2 Missing Data Troubleshooting ==="
echo

# Function to check PX4 parameter
 check_param() {
    local param_name=$1
    echo "Checking $param_name..."
    # This would need to be run in PX4 shell environment
    echo "param show $param_name"
}

# Function to check sensor status
check_sensors() {
    echo "1. Checking sensor status..."
    echo "   Run in PX4 shell: sensors status"
    echo "   Run in PX4 shell: ekf2 status"
    echo
}

# Function to check EKF2 configuration
check_ekf2_config() {
    echo "2. Checking EKF2 configuration..."
    echo "   Parameters to check:"
    echo "   - EKF2_EN: Should be 1"
    echo "   - EKF2_MULTI_IMU: Usually 1 for single IMU"
    echo "   - SENS_IMU_MODE: Usually 1 for single EKF"
    echo "   - EKF2_MAG_TYPE: Should be 1"
    echo "   - EKF2_GPS_CTRL: Should be 7 for GPS+GLONASS+Galileo"
    echo "   - EKF2_BARO_CTRL: Should be 1"
    echo
}

# Function to check sensor calibration
check_calibration() {
    echo "3. Checking sensor calibration..."
    echo "   Run in PX4 shell:"
    echo "   - cal status"
    echo "   - Check if all sensors show as calibrated"
    echo "   - If not calibrated, run: calibrate"
    echo
}

# Function to provide quick fixes
quick_fixes() {
    echo "4. Quick fixes to try:"
    echo "   a) Reset EKF2 parameters to defaults:"
    echo "      param reset EKF2_EN"
    echo "      param reset EKF2_MULTI_IMU"
    echo "      param reset SENS_IMU_MODE"
    echo "   b) Force sensor calibration:"
    echo "      param set SYS_CAL_ACCEL 1"
    echo "      param set SYS_CAL_GYRO 1"
    echo "      param set SYS_CAL_MAG 1"
    echo "      param set SYS_CAL_BARO 1"
    echo "   c) Allow arming without GPS (if GPS is issue):"
    echo "      param set COM_ARM_WO_GPS 1"
    echo "   d) Reboot system:"
    echo "      reboot"
    echo
}

# Function to check specific error conditions
check_specific_conditions() {
    echo "5. Checking specific error conditions:"
    echo "   - If using multiple IMUs, ensure SENS_IMU_MODE=0 and EKF2_MULTI_IMU>1"
    echo "   - If GPS issues, check: EKF2_GPS_CHECK and GPS quality"
    echo "   - If magnetometer issues, check: EKF2_MAG_CHECK and magnetic interference"
    echo "   - If barometer issues, check: EKF2_BARO_CTRL and baro health"
    echo
}

# Main troubleshooting flow
main() {
    check_sensors
    check_ekf2_config
    check_calibration
    quick_fixes
    check_specific_conditions

    echo "=== Additional Resources ==="
    echo "- PX4 EKF2 documentation: https://docs.px4.io/main/en/advanced_config/tuning_the_ecl_ekf.html"
    echo "- Check flight logs for detailed error messages"
    echo "- Use QGroundControl for parameter management"
    echo "- Monitor MAVLink messages for detailed diagnostics"
    echo
    echo "=== Next Steps ==="
    echo "1. Run the diagnostic commands in PX4 shell"
    echo "2. Check parameter values and adjust if needed"
    echo "3. Calibrate sensors if required"
    echo "4. Test after each change"
    echo "5. If problem persists, collect flight logs for analysis"
}

# Run main function
main
